/********************************************************************************************************
 * @file ResultCode.java
 *
 * @brief for TLSR chips
 *
 * @author telink
 * @date Sep. 30, 2019
 *
 * @par Copyright (c) 2019, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *
 *          Licensed under the Apache License, Version 2.0 (the "License");
 *          you may not use this file except in compliance with the License.
 *          You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 *          Unless required by applicable law or agreed to in writing, software
 *          distributed under the License is distributed on an "AS IS" BASIS,
 *          WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *          See the License for the specific language governing permissions and
 *          limitations under the License.
 *******************************************************************************************************/
package com.telink.ota.foundation;

public enum ResultCode {

    /**
     * success
     */
    OTA_SUCCESS(0x00, "success"),

    /***************************************************************
     * fail codes and info
     ***************************************************************/
    OTA_DATA_PACKET_SEQ_ERR(0x01, "OTA data packet sequence number error: repeated OTA PDU or lost some OTA PDU"),

    OTA_PACKET_INVALID(0x02, "invalid OTA packet: 1. invalid OTA command; 2. addr_index out of range; 3.not standard OTA PDU length"),

    OTA_DATA_CRC_ERR(0x03, "packet PDU CRC err"),

    OTA_WRITE_FLASH_ERR(0x04, "write OTA data to flash ERR"),

    OTA_DATA_INCOMPLETE(0x05, "lost last one or more OTA PDU"),

    OTA_FLOW_ERR(0x06, "peer device send OTA command or OTA data not in correct flow"),

    OTA_FW_CHECK_ERR(0x07, "firmware CRC check error"),

    OTA_VERSION_COMPARE_ERR(0x08, "the version number to be update is lower than the current version"),

    OTA_PDU_LEN_ERR(0x09, "PDU length error: not 16*n, or not equal to the value it declare in \"CMD_OTA_START_EXT\" packet"),

    OTA_FIRMWARE_MARK_ERR(0x0a, "firmware mark error: not generated by telink's BLE SDK"),

    OTA_FW_SIZE_ERR(0x0b, "firmware size error: no firmware_size; firmware size too small or too big"),

    OTA_DATA_PACKET_TIMEOUT(0x0c, "time interval between two consequent packet exceed a value(user can adjust this value)"),

    OTA_TIMEOUT(0x0d, "OTA flow total timeout"),

    OTA_FAIL_DUE_TO_CONNECTION_TERMINATE(0x0e, "OTA fail due to current connection terminate(maybe connection timeout or local/peer device terminate connection)"),


    /* only secure boot mode involved form 0x80 */
    OTA_SECBOOT_HW_ERR(0x80, "OTA server device hardware error"),

    OTA_SECBOOT_SYSTEM_ERR(0x81, "OTA server device system error"),

    OTA_SECBOOT_FUNC_NOT_ENABLE(0x82, "OTA server device do not enable secure boot function"),

    OTA_SECBOOT_PUBKEY_SIGN_SEQ_ERR(0x83, "OTA public key & signature sequence number error: repeated or lost"),

    OTA_SECBOOT_PUBKEY_SIGN_LEN_ERR(0x84, "OTA public key & signature data packet length error"),

    OTA_SECBOOT_PUBLIC_KEY_ERR(0x85, "OTA client public key not match OTA server device local hash"),

    OTA_SECBOOT_NO_SIGN_BEFORE_OTA_PDU(0x86, "OTA client do not send Signature data before OTA firmware PDU"),

    OTA_SECBOOT_SIGN_VERIFY_FAIL(0x87, "OTA signature verification fail"),


    UNKNOWN_CODE(0xFF, "unknown code"),

    ;

    public final int value;

    public final String info;

    ResultCode(int value, String info) {
        this.value = value;
        this.info = info;
    }

    public static ResultCode getResultCode(int code) {
        for (ResultCode resultCode : values()) {
            if (code == resultCode.value) {
                return resultCode;
            }
        }
        return UNKNOWN_CODE;
    }

    @Override
    public String toString() {
        if (this == OTA_SUCCESS) {
            return "OTA success";
        }
        return String.format("OTA result: %02X - %s", value, info);
    }
}
